# vim: set fileencoding=UTF-8 :

# ------------------------------------------------------------
# ::                        WIGA-SOFT                       ::
# ------------------------------------------------------------
# charts.py
# Functions for generating various charts used in reports
# Created by: Vycas
# Version: 1.0
# ------------------------------------------------------------

from pygooglechart import *

monthNames = ('Sau', 'Vas', 'Kov', 'Bal', 'Geg', 'Bir', 'Lie', 'Rug', 'Rug', 'Spa', 'Lap', 'Gru')
#shortMonthNames = ('S', 'V', 'K', 'B', 'G', 'B', 'L', 'R', 'R', 'S', 'L', 'G')
shortMonthNames = ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12')

green = '5B8A40' #'5f8e3f' #'92d15e' #'1FD700'
red = 'ef4223' #'ff9975' #'FF2626'
yellow = 'd7df23' #'a4846f' #f9e351' #'FFCE00'
defaultWidth = 750
defaultHeight = 300
legendPosition = 'r'
fixedLimit = 26
fixedWidth = 23
shortNamesLimit = 32

def intensityChart(list, title=None, width=defaultWidth, height=defaultHeight, localColor=green, foreignColor=yellow):
    try:
        legend = ['Mano skyrius', 'Kiti skyriai']
        colors = [localColor, foreignColor]
        months = [x['date'].month for x in list]
        years = [x['date'].strftime('%Y') for x in list]
        local = [x['local'] for x in list]
        foreign = [x['foreign'] for x in list]
        y_max = max([x['local'] + x['foreign'] for x in list])
        chart = StackedVerticalBarChart(width, height, title, legend, colors, y_range=(0,y_max))
        chart.set_legend_position(legendPosition)
        chart.set_axis_range(Axis.LEFT, 0, y_max)
        if len(list) < shortNamesLimit:
            correctDates(months, years, monthNames)
        else:
            correctDates(months, years, shortMonthNames)
        chart.set_axis_labels(Axis.BOTTOM, months)
        chart.set_axis_labels(Axis.BOTTOM, years)
        chart.add_data(local)
        chart.add_data(foreign)
        if len(list) < fixedLimit:
            chart.set_bar_width(fixedWidth)
        return chart.get_url()
    except:
        return None

def budgetChart(list, title=None, width=defaultWidth, height=defaultHeight, workhoursColor=yellow, budgetColor=green, overColor=red):
    try:
        legend = ['Išnaudota', 'Liko', 'Viršyta']
        colors = [workhoursColor, budgetColor, overColor]
        months = [x['date'].month for x in list]
        years = [x['date'].strftime('%Y') for x in list]
        used = [min(x['total'], x['used']) for x in list]
        left = [max(x['left'], 0) for x in list]
        over = [(min(x['left'], 0) * -1) for x in list]
        y_max = max([max(x['used'], x['total']) for x in list])
        chart = StackedVerticalBarChart(width, height, title, legend, colors, y_range=(0,y_max))
        chart.set_legend_position(legendPosition)
        chart.set_axis_range(Axis.LEFT, 0, y_max)
        if len(list) < shortNamesLimit:
            correctDates(months, years, monthNames)
        else:
            correctDates(months, years, shortMonthNames)
        chart.set_axis_labels(Axis.BOTTOM, months)
        chart.set_axis_labels(Axis.BOTTOM, years)
        chart.add_data(used)
        chart.add_data(left)
        chart.add_data(over)
        if len(list) < fixedLimit:
            chart.set_bar_width(fixedWidth)
        return chart.get_url()
    except:
        return None

def loadChart(list, title=None, width=defaultWidth, height=defaultHeight, budgetColor=green, resourceColor=red):
    try:
        legend = ['Padengti resursai', 'Nepadengti resursai']
        colors = [budgetColor, resourceColor]
        months = [x['date'].month for x in list]
        years = [x['date'].strftime('%Y') for x in list]
        budget = [x['budget'] for x in list]
        noBudget = [max(x['resource'] - x['budget'], 0) for x in list]
        y_max = max([x['resource'] for x in list])
        chart = StackedVerticalBarChart(width, height, title, legend, colors, y_range=(0,y_max))
        chart.set_legend_position(legendPosition)
        chart.set_axis_range(Axis.LEFT, 0, y_max)
        if len(list) < shortNamesLimit:
            correctDates(months, years, monthNames)
        else:
            correctDates(months, years, shortMonthNames)
        chart.set_axis_labels(Axis.BOTTOM, months)
        chart.set_axis_labels(Axis.BOTTOM, years)
        chart.add_data(budget)
        chart.add_data(noBudget)
        if len(list) < fixedLimit:
            chart.set_bar_width(fixedWidth)
        #chart.add_marker(0, -1, 'N*f0', 'FFFFFF', 10)
        #chart.add_marker(1, -1, 'N*f0', '000000', 10)
        return chart.get_url()
    except:
        return None

def correctDates(months, years, names):
    try:
        for i in range(1, len(years)-1):
            if months[i] != 1:
                years[i] = ''
        if years[1] != '':
            years[0] = ''
        if years[-2] != '':
            years[-1] = ''
    except IndexError:
        pass
    for i in range(0, len(months)):
        months[i] = names[months[i]-1]
